<?php
ifi!$_GET['cmd']) {
	if(!perms_check('users', 'gr_edit')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	SQLvalidate($_GET['rec']);
	if(!$_GET['rec'])
		$_GET['rec']=0;

	$fields=array('id', 'name', 'points', 'path');

	//sort
	if($_GET['sort']!='ASC' && $_GET['sort'] != 'DESC')
		$_GET['sort']='DESC';
	if(!$_GET['sortby'] or !in_array($_GET['sortby'], $fields))
		$_GET['sortby']='id';
	if($_GET['sort']=='ASC')
		$sortnext='DESC';
	else
		$sortnext='ASC';
	//

	if($_GET['sortby'] == 'name')
		$_GET['sortby'] .= "_$_SESSION[lang_short]";

	$content = string_template(read_file('admin/themes/ranks_header.php'), array("sortnext" => $sortnext));

	$zapytanie = "SELECT * FROM $conf[prefix_ranks]ranks";

	if($_GET['sortby'] == 'name')
		$_GET['sortby'] .= "_$_SESSION[lang_short]";

	if($_GET['sort'])
		$zapytanie.= " ORDER BY ".$_GET['sortby']." $_GET[sort]";
	else
		$zapytanie.= " ORDER BY id ASC";

	$db->query($zapytanie) or $db->err(__FILE__, __LINE__);

	while($rank = $db->fetch_object()) {
		$i++;

		$nm = "name_$_SESSION[lang_short]";

		if($rank->$nm == '') {
			$nm = "name_".$conf['language'];
		}

		$name = $rank->$nm;
		$rank->path = $conf['images_dir'].$rank->path;

		$content .= string_template(read_file('admin/themes/ranks_item.php'), array("id" => $rank->id, "name" => add_dots(strip_tags(stripslashes($name)), 30), "path" => $rank->path, "i" => (($i%2)+1), 'points' => $rank->points));
	}
	$content .= read_file('admin/themes/ranks_footer.php');
}
elseif($_GET['cmd']=='edit') {
	SQLvalidate($_GET['id']);
	if(!perms_check('users', 'gr_edit')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$db->query("SELECT * FROM $conf[prefix_ranks]ranks WHERE id=$_GET[id]") or $db->err(__FILE__, __LINE__);

	//sprawdzanie czy ranga istnieje
	if($db->num_rows() == 0) {
		redirect('index.php?module=admin&action=ranks');
		exit;
	}
	//

	$d=$db->fetch_object();

	$d->image=stripslashes($d->image);

	$array = array('function' => 'rank_edit', 'id' => $d->id, 'points' => $d->points, 'path' => $d->path, 'submit' => $lang['admin_edit']);

	$multi = NULL;

	$langs = get_lang_list();
	$languages = get_languages_list();

	$i = 0;

	foreach($langs as $key => $l) {
		$i++;
		if($l==$_SESSION['lang_short']) {
			$icon = 'collapse';
			$style = 'block';
		}
		else {
			$icon = 'expand';
			$style = 'none';
		}

		$multi .= string_template(read_file('admin/themes/ranks_form_multi.php'), array('lang' => $l, 'language' => strtoupper($l), 'icon' => $GLOBALS['theme_path'].$GLOBALS['theme_img'][$icon], 'style' => $style, 'i' => $i));
		$nm = "name_$l";
		$array[$nm] = stripslashes($d->$nm);
	}

	$content = string_template(read_file('admin/themes/ranks_form.php'), array('multi' => $multi));
	$content = string_template($content, $array);
}
elseif($_GET['cmd'] == 'delete') {
	SQLvalidate($_GET['id']);
	if(!perms_check('users', 'gr_edit')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$db->query("SELECT * FROM $conf[prefix_ranks]ranks WHERE id=$_GET[id]") or $db->err(__FILE__, __LINE__);

	//sprawdzanie czy grupa istnieje
	if( $db->num_rows() == 0 ) {
		redirect('index.php?module=admin&action=ranks');
		exit;
	}

	$d = $db->fetch_object();

	$yes='<input type="button" onClick="javascript:location.href=\'index.php?module=ranks&function=rank_delete&amp;rank='.$d->id.'\'" value="'.$lang['yes'].'" class="button">';
	$no='<input type="button" onClick="javascript:location.href=\'index.php?module=admin&amp;action=ranks\'" value="'.$lang['no'].'" class="button">';

	$nm = "name_$_SESSION[lang_short]";

	if($d->$nm == '') {
		$nm = "name_".$conf['language'];
	}

	$t = $d->$nm;

	$content = string_template($lang['admin_really_delete'], array('yes' => $yes, 'no' => $no, 'what' => read_text_rest($t)));
}
elseif($_GET['cmd'] == 'add') {
	if(!perms_check('users', 'gr_add')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$array = array('function' => 'rank_add', 'path' => '', 'points' => 0, 'submit' => $lang['admin_add']);

	$multi = NULL;

	$langs = get_lang_list();
	$languages = get_languages_list();

	$i = 0;

	foreach($langs as $key => $l) {
		$i++;
		if($l==$_SESSION['lang_short']) {
			$icon = 'collapse';
			$style = 'block';
		}
		else {
			$icon = 'expand';
			$style = 'none';
		}

		$multi .= string_template(read_file('admin/themes/ranks_form_multi.php'), array('lang' => $l, 'language' => strtoupper($l), 'icon' => $GLOBALS['theme_path'].$GLOBALS['theme_img'][$icon], 'style' => $style, 'i' => $i));
		$array["name_$l"] = '';
	}

	$content = string_template(read_file('admin/themes/ranks_form.php'), array('multi' => $multi));
	$content = string_template($content, $array);
}

$content=string_template(read_file('middle.php'), array('theme_path' => $GLOBALS['theme_path'], 'content' => $content, 'description' => $lang['ranks']));
//

?>
